package com.onegroup.service.impl;

import com.onegroup.common.enums.ResponseStatusEnum;
import com.onegroup.common.exception.MyCustomException;
import com.onegroup.common.utils.JwtUtil;
import com.onegroup.common.utils.RedisCache;
import com.onegroup.mapper.ApplicationMapper;
import com.onegroup.mapper.DeptHostMapper;
import com.onegroup.mapper.FinanceMapper;
import com.onegroup.pojo.*;
import com.onegroup.pojo.dto.ApplicationDto;
import com.onegroup.service.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author: 曾雪斌
 * @Description: 统一身份认证管理service实现层
 * @History:
 */
@Service
@Transactional
public class AuthenticationServiceImpl implements AuthenticationService {

    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private DeptHostMapper deptHostMapper;
    @Autowired
    private FinanceMapper financeMapper;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private RedisCache redisCache;

    @Override
    public void registerApplication(Application application) {

    }

    /**
     * 申报人员登录
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @Override
    public ResponseResult<Map<String, String>> loginApplicationByPwd(String username, String password) {
        // auth方法校验
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
        Authentication authenticate = authenticationManager.authenticate(token);

        // 校验失败
        if (Objects.isNull(authenticate)) {
            throw new MyCustomException(ResponseStatusEnum.USER_ERROR);
        }

        //生成jwt
        ApplicationDto applicationDto = (ApplicationDto) authenticate.getPrincipal();
        String appId = applicationDto.getApplication().getAppId().toString();
        String jwt = JwtUtil.createJwt(appId);

        HashMap<String, String> map = new HashMap<>();
        map.put("token", jwt);

        // 存入redis
        redisCache.setCacheObject("login:application:" + appId, applicationDto);
        return new ResponseResult<>(200, "success", "登录成功！", map);
    }

    /**
     * 申报人员退出登录
     * @return
     */
    @Override
    public ResponseResult<String> logoutApplication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        ApplicationDto applicationDto = (ApplicationDto) authentication.getPrincipal();
        Integer appId = applicationDto.getApplication().getAppId();
        redisCache.deleteObject("login:application:" + appId);
        return ResponseResult.success("退出成功！");
    }

    @Override
    public Application loginApplicationByPin(String email, String pin) {
        return null;
    }

    @Override
    public Finance loginFinance(String username, String password) {
        return null;
    }

    @Override
    public DeptHost loginDeptHost(String username, String password) {
        return null;
    }

    @Override
    public Expert loginExpert(String username, String password) {
        return null;
    }

    @Override
    public void forgotPassword(String username, String idCard, String emailPin, String newPwd) {

    }

}
